package com.itheima.reggie.common;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JacksonObjectMapper extends ObjectMapper {

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
    public JacksonObjectMapper() {
        super();
        //反序列化的时候如果多了其他属性，不报异常 这些常量都是枚举，类型为boolean
        this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);

        //反序列化的时候，属性不存在的异常处理
        this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        //自定义转换 序列化器
        SimpleModule simpleModule=new SimpleModule()
                .addSerializer(BigInteger.class, ToStringSerializer.instance)
                .addSerializer(Long.class,ToStringSerializer.instance)
                //日期序列化器不使用时间戳类型

                .addSerializer(Date.class, new DateSerializer
                        (false,new SimpleDateFormat(DEFAULT_DATE_FORMAT)));
    this.registerModule(simpleModule);
    }
}
